package com.waner.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waner.reggie.common.R;
import com.waner.reggie.dto.DishDto;
import com.waner.reggie.dto.SetmealDto;
import com.waner.reggie.entity.Dish;
import com.waner.reggie.entity.Setmeal;
import com.waner.reggie.entity.SetmealDish;
import com.waner.reggie.service.DishService;
import com.waner.reggie.service.SetmealDishService;
import com.waner.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;


    /**
     * 套餐的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){

        Page<SetmealDto> pageInfo = setmealService.pageWithCategoryName(page,pageSize,name);

        return R.success(pageInfo);
    }

    /**
     * 新增套餐操作
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("新增套餐...");
        setmealService.saveWithDishes(setmealDto);
        return R.success("新增套餐成功！");
    }

    /**
     * 修改套餐状态
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> forSale(@PathVariable int status, @RequestParam List<Long> ids){
        log.info("修改套餐状态...");

        // 根据id进行查询
        for(Long id:ids){
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }

        return R.success("更新成功！");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        // delete(List<Long>) 也可以
        log.info("删除套餐操作...");
        setmealService.deleteWithDishes(ids);
        return R.success("删除套餐成功！");
    }

    /**
     * 根据id查询套餐以及对应的菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){

        log.info("根据id查询套餐信息...");

        SetmealDto setmealDto = setmealService.getWithdishedById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息，以及相关菜品信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("修改套餐数据...");

        setmealService.updateWithDishes(setmealDto);

        return R.success("修改套餐数据成功！");
    }

    /**
     * 根据套餐分类查询套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId, int status){
        log.info("查询套餐");
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId)
                .eq(Setmeal::getStatus,status)
                .orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 根据setmealId查询对应的菜品
     * @param setmealId
     * @return
     */
    @GetMapping("/dish/{setmealId}")
    public R<List<DishDto>> getDishes(@PathVariable Long setmealId){
        log.info("套餐的详情..");
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);

        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        List<DishDto> dishDtos = list.stream().map(setmealDish -> {
            DishDto dishDto = dishService.getDishByIdWithFlavor(setmealDish.getDishId());
            dishDto.setCopies(setmealDish.getCopies());
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtos);
    }

}
